package psha256

import (
	"crypto/md5"
	"crypto/sha256"
	"fmt"

	"gitee.com/daledi/public/encoding/pbase64"
)

func Encrypt(vals ...interface{}) []byte {
	if len(vals) == 0 {
		return nil
	}
	m := sha256.New()
	for i := range vals {
		switch v := vals[i].(type) {
		case string:
			m.Write([]byte(v))
		case []byte:
			m.Write(v)
		}
	}
	return m.Sum(nil)[:md5.Size]
}

func EncryptString(vals ...interface{}) string {
	return fmt.Sprintf("%02x", Encrypt(vals...))
}

func EncryptB64(vals ...interface{}) string {
	return pbase64.Encode(Encrypt(vals...))
}
